package com.ruoyi.college.service.impl;

import java.util.List;

import com.ruoyi.college.domain.*;
import com.ruoyi.college.domain.Class;
import com.ruoyi.college.service.*;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.college.mapper.ExperimentReportMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 实验报告管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-31
 */
@Service
public class ExperimentReportServiceImpl implements IExperimentReportService 
{
    @Autowired
    private ExperimentReportMapper experimentReportMapper;

    /**
     * 查询实验报告管理
     * 
     * @param reportId 实验报告管理主键
     * @return 实验报告管理
     */
    @Override
    public ExperimentReport selectExperimentReportByReportId(Long reportId)
    {
        ExperimentReport item = experimentReportMapper.selectExperimentReportByReportId(reportId);
        Long classProjectId = item.getClassProjectId();
        ExperimentClassProject experimentClassProject = experimentClassProjectService.selectExperimentClassProjectByClassProjectId(classProjectId);
        //实验项目信息
        Long projectId = experimentClassProject.getProjectId();
        ExperimentProject experimentProject = experimentProjectService.getProjectByProjectId(projectId);
        item.setProjectId(experimentProject.getProjectId());
        item.setProjectName(experimentProject.getProjectName());
        //班级信息
        Long classId = experimentClassProject.getClassId();
        Class aClass = classService.selectClassByClassId(classId);
        item.setClassId(classId);
        item.setClassName(aClass.getClassName());

        Long arrangementId = experimentClassProject.getArrangementId();
        CourseArrangement courseArrangement = courseArrangementService.selectCourseArrangementByArrangementId(arrangementId);
        //课程信息
        Long courseId = courseArrangement.getCourseId();
        Course course = courseService.selectCourseByCourseId(courseId);
        item.setCourseId(courseId);
        item.setCourseName(course.getCourseName());
        //学期信息
        Long semesterId = courseArrangement.getSemesterId();
        Semester semester = semesterService.selectSemesterBySemesterId(semesterId);
        item.setSemesterId(semesterId);
        item.setSemesterName(semester.getSemesterName());
        SysUser sysUser = sysUserService.selectUserById(item.getStudentId());
        item.setStudentName(sysUser.getUserName());
        return item;
    }

    @Autowired
    private IExperimentClassProjectService experimentClassProjectService;

    @Autowired
    private IExperimentProjectService experimentProjectService;

    @Autowired
    private ICourseArrangementService courseArrangementService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private ISemesterService semesterService;

    @Autowired
    private IClassService classService;

    @Autowired
    private ISysUserService  sysUserService;

    /**
     * 查询实验报告管理列表
     * 
     * @param experimentReport 实验报告管理
     * @return 实验报告管理
     */
    @Override
    public List<ExperimentReport> selectExperimentReportList(ExperimentReport experimentReport)
    {
        List<ExperimentReport> experimentReports = experimentReportMapper.selectExperimentReportList(experimentReport);
        experimentReports.forEach(item->{
            Long classProjectId = item.getClassProjectId();
            ExperimentClassProject experimentClassProject = experimentClassProjectService.selectExperimentClassProjectByClassProjectId(classProjectId);
            //实验项目信息
            Long projectId = experimentClassProject.getProjectId();
            ExperimentProject experimentProject = experimentProjectService.getProjectByProjectId(projectId);
            item.setProjectId(experimentProject.getProjectId());
            item.setProjectName(experimentProject.getProjectName());
            //班级信息
            Long classId = experimentClassProject.getClassId();
            Class aClass = classService.selectClassByClassId(classId);
            item.setClassId(classId);
            item.setClassName(aClass.getClassName());

            Long arrangementId = experimentClassProject.getArrangementId();
            CourseArrangement courseArrangement = courseArrangementService.selectCourseArrangementByArrangementId(arrangementId);
            //课程信息
            Long courseId = courseArrangement.getCourseId();
            Course course = courseService.selectCourseByCourseId(courseId);
            item.setCourseId(courseId);
            item.setCourseName(course.getCourseName());
            //学期信息
            Long semesterId = courseArrangement.getSemesterId();
            Semester semester = semesterService.selectSemesterBySemesterId(semesterId);
            item.setSemesterId(semesterId);
            item.setSemesterName(semester.getSemesterName());
            SysUser sysUser = sysUserService.selectUserById(item.getStudentId());
            item.setStudentName(sysUser.getUserName());
        });
        return experimentReports;
    }

    /**
     * 新增实验报告管理
     * 
     * @param experimentReport 实验报告管理
     * @return 结果
     */
    @Override
    public int insertExperimentReport(ExperimentReport experimentReport)
    {
        experimentReport.setCreateTime(DateUtils.getNowDate());
        return experimentReportMapper.insertExperimentReport(experimentReport);
    }

    /**
     * 上传路径
     */
    @Value("${ruoyi.profile}")
    private String profile;

    /**
     * 报告附件上传路径
     */
    private static final String REPORT_UPLOAD_PATH = "/report/";

    /**
     * 上传报告附件
     */
    @Override
    public String uploadReportFile(MultipartFile file) throws Exception {
        // 验证文件类型
        String fileExt = FileUploadUtils.getExtension(file);
        //if (!MimeTypeUtils.isAllowedExtension(fileExt,
        //        MimeTypeUtils.DOC, MimeTypeUtils.DOCX, MimeTypeUtils.PDF,
        //        MimeTypeUtils.ZIP, MimeTypeUtils.RAR, MimeTypeUtils.TXT)) {
        //    throw new Exception("不支持的文件格式，请上传.doc, .docx, .pdf, .zip, .rar, .txt格式");
        //}

        // 上传文件
        String fileName = FileUploadUtils.upload(profile + REPORT_UPLOAD_PATH, file);
        // 返回相对路径（存储到数据库）
        return REPORT_UPLOAD_PATH + fileName;
    }

    @Override
    public int saveReport(ExperimentReport report, MultipartFile file) throws Exception {
        // 处理附件上传
        if (file != null && !file.isEmpty()) {
            String filePath = uploadReportFile(file);
            report.setFilePath(filePath);
        }
        // 设置创建人信息
        report.setCreateBy(ShiroUtils.getSysUser().getUserName());
        report.setCreateTime(DateUtils.getNowDate());

        // 先查询当前学生有没有提交报告，如果已经提交，直接覆盖，否则保存
        ExperimentReport oldReport = experimentReportMapper.selectExperimentReportByStudentIdAndClassProjectId(report.getStudentId(), report.getClassProjectId());
        if (oldReport == null){
            return experimentReportMapper.insertExperimentReport(report);
        }

        report.setReportId(oldReport.getReportId());
        return experimentReportMapper.updateExperimentReport(report);
    }

    /**
     * 修改实验报告管理
     * 
     * @param experimentReport 实验报告管理
     * @return 结果
     */
    @Override
    public int updateExperimentReport(ExperimentReport experimentReport)
    {
        experimentReport.setUpdateTime(DateUtils.getNowDate());
        //学生提交时间
        experimentReport.setSubmitTime(DateUtils.getNowDate());
        return experimentReportMapper.updateExperimentReport(experimentReport);
    }

    @Override
    public int updateExperimentReviewReport(ExperimentReport experimentReport) {
        experimentReport.setUpdateTime(DateUtils.getNowDate());
        //老师批阅时间
        experimentReport.setReviewTime(DateUtils.getNowDate());
        return experimentReportMapper.updateExperimentReport(experimentReport);
    }

    /**
     * 批量删除实验报告管理
     * 
     * @param reportIds 需要删除的实验报告管理主键
     * @return 结果
     */
    @Override
    public int deleteExperimentReportByReportIds(String reportIds)
    {
        return experimentReportMapper.deleteExperimentReportByReportIds(Convert.toStrArray(reportIds));
    }

    /**
     * 删除实验报告管理信息
     * 
     * @param reportId 实验报告管理主键
     * @return 结果
     */
    @Override
    public int deleteExperimentReportByReportId(Long reportId)
    {
        return experimentReportMapper.deleteExperimentReportByReportId(reportId);
    }
}
